package com.example.hotel;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.example.hotel.R.color;

import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewDebug.FlagToString;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Add_cart extends Activity implements OnClickListener {
	final ArrayList<String> product_id1 = new ArrayList<String>();
	final ArrayList<String> product_name = new ArrayList<String>();
	final ArrayList<String> type_product = new ArrayList<String>();
	final ArrayList<String> price_product = new ArrayList<String>();
	final ArrayList<String> qty_product = new ArrayList<String>();
	final ArrayList<String> total = new ArrayList<String>();
	TextView wellcome; // DataShow
	Boolean flag = true;
	private ListView listViewData;
	TextView tv_total_price, tv_Product_name, tv_Type, tv_Price, tv_QTY,
			tv_Total, tv_Update, tv_Remove, tv_Total_Payment;
	String nameCart = "";
	Double total_price = 0.00;
	DecimalFormat decimalFormat;
	ArrayList<HashMap<String, String>> myArrList;
	ImageView back_home, image_cart, image_msg, image_bill, imageback;
	Button continue_Services, continue_Shopping, check_out;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_cart);
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
		TextView tvAmount = (TextView) findViewById(R.id.tvAmount);
		tv_total_price = (TextView) findViewById(R.id.tv_total_price);
		tv_Total_Payment = (TextView) findViewById(R.id.tv_Total_Payment);
		tv_Product_name = (TextView) findViewById(R.id.tv_Product_name);
		tv_Type = (TextView) findViewById(R.id.tv_Type);
		tv_Price = (TextView) findViewById(R.id.tv_Price);
		tv_QTY = (TextView) findViewById(R.id.tv_QTY);
		tv_Total = (TextView) findViewById(R.id.tv_Total);
		tv_Update = (TextView) findViewById(R.id.tv_Update);
		tv_Remove = (TextView) findViewById(R.id.tv_Remove);

		wellcome = (TextView) findViewById(R.id.tvWellcome);
		image_cart = (ImageView) findViewById(R.id.image_cart);
		image_msg = (ImageView) findViewById(R.id.image_msg);
		image_bill = (ImageView) findViewById(R.id.image_bill);
		imageback = (ImageView) findViewById(R.id.imageback);
		back_home = (ImageView) findViewById(R.id.imagehome);
		continue_Services = (Button) findViewById(R.id.continue_Services);
		continue_Shopping = (Button) findViewById(R.id.continue_Shopping);
		check_out = (Button) findViewById(R.id.check_out);
		continue_Services.setOnClickListener(this);
		continue_Shopping.setOnClickListener(this);
		check_out.setOnClickListener(this);
		image_msg.setOnClickListener(this);
		imageback.setOnClickListener(this);
		image_cart.setOnClickListener(this);
		image_bill.setOnClickListener(this);
		back_home.setOnClickListener(this);
		wellcome.setText(((Global) getApplicationContext()).wellcome);
		myArrList = new ArrayList<HashMap<String, String>>();
		nameCart = ((Global) getApplicationContext()).nameCart;
		Boolean flag_call = ((Global) getApplicationContext()).callpage_bool;
		Boolean flag_call1 = ((Global) getApplicationContext()).page_service;
		if (flag_call) {
			if (flag_call1) {
				imageback.setImageDrawable(getResources().getDrawable(
						R.drawable.arrow_upright));
				imageback.setOnClickListener(this);
			}
			else
			{
				((Global) getApplicationContext()).page_service = false;
			}
		} else {
			((Global) getApplicationContext()).callpage_bool = false;
			
		}

		if (nameCart.equals("edit_cart")) {
			getEdit_cart();
		} else if (nameCart.equals("show_cart")) {
			getShow_cart();
		} else if (nameCart.equals("del_cart")) {
			getRemove_cart();
		}
		if (flag) {

			decimalFormat = new DecimalFormat("###,##0.00");
			tv_total_price.setText(decimalFormat.format(total_price)
					+ "  TH(BATH)");
			HashMap<String, String> map;
			// String Servicesname = ((Global)
			// getApplicationContext()).Servicesname;
			// product_id1.size()
			for (int i = 0; i < product_id1.size(); i++) {
				map = new HashMap<String, String>();
				map.put("id", product_id1.get(i));
				map.put("product_name", product_name.get(i));
				map.put("type_product", type_product.get(i));
				map.put("price_product", price_product.get(i));
				map.put("qty_product", qty_product.get(i));
				map.put("total", total.get(i));
				myArrList.add(map);
			}

			tvAmount.setText("("
					+ ((Global) getApplicationContext()).cart_count.toString()
					+ ")");
			listViewData = (ListView) findViewById(R.id.listView1);
			listViewData.setAdapter(new AdapterListViewData(this));
		} else {
			check_out.setEnabled(false);

		}
		/*------setfonts------*/

		Typeface Font = Typeface.createFromAsset(getAssets(),
				"fonts/PSL-Kittithada.ttf");
		Typeface FontBold = Typeface.createFromAsset(getAssets(),
				"fonts/PSL-KittithadaBold.ttf");
		TextView tv_amount = (TextView) findViewById(R.id.tvAmount);
		tv_amount.setTypeface(Font);
		wellcome.setTypeface(FontBold);
		tv_Product_name.setTypeface(FontBold);
		tv_Price.setTypeface(FontBold);
		tv_Type.setTypeface(FontBold);
		tv_QTY.setTypeface(FontBold);
		tv_Total.setTypeface(FontBold);
		tv_Update.setTypeface(FontBold);
		tv_Remove.setTypeface(FontBold);
		tv_Total_Payment.setTypeface(FontBold);
		tv_total_price.setTypeface(FontBold);
		continue_Services.setTypeface(FontBold);
		continue_Shopping.setTypeface(FontBold);
		check_out.setTypeface(FontBold);
		/*--------------------*/

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imagehome:
			startActivity(new Intent(Add_cart.this, home.class));
			break;
		case R.id.continue_Services:
			startActivity(new Intent(Add_cart.this, Service.class));
			break;
		case R.id.continue_Shopping:
			startActivity(new Intent(Add_cart.this, Room_shopping.class));
			break;
		case R.id.check_out:
			((Global) getApplicationContext()).cart_count = 0;
			startActivity(new Intent(Add_cart.this, Check_out.class));
			break;
		case R.id.image_cart:
			((Global) getApplicationContext()).nameCart = "show_cart";
			((Global) getApplicationContext()).callpage1 = com.example.hotel.Add_cart.class;
			startActivity(new Intent(Add_cart.this, Add_cart.class));
			break;
		case R.id.image_msg:
			((Global) getApplicationContext()).callpage1 = com.example.hotel.Add_cart.class;
			startActivity(new Intent(Add_cart.this, Message_list.class));
			break;
		case R.id.image_bill:
			((Global) getApplicationContext()).callpage1 = com.example.hotel.Add_cart.class;
			startActivity(new Intent(Add_cart.this, Billing_history.class));
			break;
		case R.id.imageback:
			Class myClass = ((Global) getApplicationContext()).callpage1;
			Intent intent = new Intent(Add_cart.this, myClass);
			startActivity(intent);
			break;

		}
		// TODO Auto-generated method stub

	}

	public class AdapterListViewData extends BaseAdapter {

		private LayoutInflater mInflater;
		private Context context;

		public AdapterListViewData(Context c) {
			// TODO Auto-generated method stub
			context = c;
		}

		public int getCount() {
			// TODO Auto-generated method stub
			return product_id1.size();
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			// TODO Auto-generated method stub
			decimalFormat = new DecimalFormat("###,##0.00");
			final HolderListAdapter holderListAdapter;
			Typeface Font = Typeface.createFromAsset(getAssets(),
					"fonts/PSL-Kittithada.ttf");

			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.add_cart_column, null);
			}
			holderListAdapter = new HolderListAdapter();
			holderListAdapter.prodruct_name = (TextView) convertView
					.findViewById(R.id.prodruct_name);

			holderListAdapter.price = (TextView) convertView
					.findViewById(R.id.price);
			holderListAdapter.type_product = (TextView) convertView
					.findViewById(R.id.type_product);
			holderListAdapter.total = (TextView) convertView
					.findViewById(R.id.total);
			holderListAdapter.qty = (EditText) convertView
					.findViewById(R.id.qty);
			holderListAdapter.update = (ImageButton) convertView
					.findViewById(R.id.update);
			holderListAdapter.remove = (ImageButton) convertView
					.findViewById(R.id.remove);

			convertView.setTag(holderListAdapter);

			holderListAdapter.prodruct_name.setText(myArrList.get(position)
					.get("product_name"));
			holderListAdapter.prodruct_name.setTypeface(Font);
			holderListAdapter.type_product.setText(myArrList.get(position).get(
					"type_product"));
			holderListAdapter.type_product.setTypeface(Font);
			Double p = Double.parseDouble(myArrList.get(position).get(
					"price_product"));
			holderListAdapter.price.setText(decimalFormat.format(p));
			holderListAdapter.price.setTypeface(Font);
			holderListAdapter.qty.setText(myArrList.get(position).get(
					"qty_product"));
			holderListAdapter.qty.setTypeface(Font);
			holderListAdapter.total.setText(myArrList.get(position)
					.get("total"));
			holderListAdapter.total.setTypeface(Font);
			holderListAdapter.update.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					String id = myArrList.get(position).get("id");
					String qty1 = holderListAdapter.qty.getText().toString();
					if (qty1.equals("")) {

						AlertDialog.Builder builder = new AlertDialog.Builder(
								Add_cart.this);
						builder.setTitle("Order Update")
								.setIcon(
										getResources().getDrawable(
												R.drawable.ic_launcher))
								.setMessage("Please fill in the numbers.");
						AlertDialog alert = builder.create();
						alert.show();
					} else {
						((Global) getApplicationContext()).idEdit_Remove = id;
						((Global) getApplicationContext()).qty = qty1;
						((Global) getApplicationContext()).nameCart = "edit_cart";
						startActivity(new Intent(Add_cart.this, Add_cart.class));
					}
				}
			});
			holderListAdapter.remove.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					String id = myArrList.get(position).get("id");

					((Global) getApplicationContext()).idEdit_Remove = id;
					((Global) getApplicationContext()).nameCart = "del_cart";
					startActivity(new Intent(Add_cart.this, Add_cart.class));
				}
			});

			convertView.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub

				}
			});

			return convertView;
		}
	}

	public void token(String str) {
		try {
			int indexSub = str.indexOf("=");
			String b = str.substring(indexSub + 1);
			indexSub = b.indexOf(";");
			b = b.substring(0, indexSub);
			String[] a = b.split("\\|");

			if (a[0].isEmpty()) {
				flag = false;
				((Global) getApplicationContext()).cart_count = 0;
			} else {
				((Global) getApplicationContext()).cart_count = a.length;
				for (int i = 0; i < a.length; i++) {
					String[] tmp = a[i].split("\\*");
					((Global) getApplicationContext()).product_id.add(tmp[0]);
					product_id1.add(tmp[0]);
					product_name.add(tmp[1]);
					type_product.add(tmp[2]);
					price_product.add(tmp[3]);
					qty_product.add(tmp[4]);
					Double p = Double.parseDouble(tmp[5]);
					total_price += p;
					DecimalFormat decimalFormat = new DecimalFormat(
							"###,##0.00");
					total.add(decimalFormat.format(p));
				}
			}
		} catch (Exception e) {

		}
	}

	public void getEdit_cart() {
		String URL1 = ((Global) getApplicationContext()).url;
		String SOAP_ACTION = ((Global) getApplicationContext()).soap_acction
				+ nameCart;
		String NAMESPACE = "";
		String METHOD_NAME = nameCart;
		String lang = ((Global) getApplicationContext()).lang;
		String imei = ((Global) getApplicationContext()).imei;
		String idEdit_Remove = ((Global) getApplicationContext()).idEdit_Remove;
		String qty1 = ((Global) getApplicationContext()).qty;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapObject responses = null;
		request.addProperty("id", imei);
		request.addProperty("lang", lang);
		request.addProperty("product_id", idEdit_Remove);
		request.addProperty("qty", qty1);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
		androidHttpTransport.debug = true;

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			responses = (SoapObject) envelope.bodyIn;
			getShow_cart();
			// String str = responses.toString();
			// token(str);

		} catch (Exception e) {
			// Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();

		}
	}

	public void getRemove_cart() {
		String URL1 = ((Global) getApplicationContext()).url;
		String SOAP_ACTION = ((Global) getApplicationContext()).soap_acction
				+ nameCart;
		String NAMESPACE = "";
		String METHOD_NAME = nameCart;
		String lang = ((Global) getApplicationContext()).lang;
		String imei = ((Global) getApplicationContext()).imei;
		String idEdit_Remove = ((Global) getApplicationContext()).idEdit_Remove;
		String qty1 = ((Global) getApplicationContext()).qty;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapObject responses = null;
		request.addProperty("id", imei);
		request.addProperty("lang", lang);
		request.addProperty("product_id", idEdit_Remove);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
		androidHttpTransport.debug = true;

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			responses = (SoapObject) envelope.bodyIn;
			getShow_cart();
			// String str = responses.toString();
			// token(str);

		} catch (Exception e) {
			// Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();

		}
	}

	public void getShow_cart() {
		String URL1 = ((Global) getApplicationContext()).url;
		String SOAP_ACTION = ((Global) getApplicationContext()).soap_acction
				+ nameCart;
		String NAMESPACE = "";
		String METHOD_NAME = nameCart;
		String lang = ((Global) getApplicationContext()).lang;
		String imei = ((Global) getApplicationContext()).imei;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapObject responses = null;
		request.addProperty("id", imei);
		request.addProperty("lang", lang);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
		androidHttpTransport.debug = true;

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			responses = (SoapObject) envelope.bodyIn;
			String str = responses.toString();
			token(str);

		} catch (Exception e) {
			// Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();

		}
	}

}
